package academic.ui.client.presenter;


import academic.ui.client.CustomerServiceAsync;
import academic.ui.client.event.NameChangeEvent;
import academic.ui.client.event.UploadPhotoEvent;
import ecommerce.soco.shared.GWT_Customer;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.EventBus;
import com.gwtplatform.mvp.client.PresenterWidget;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.proxy.PlaceManager;
import com.gwtplatform.mvp.client.proxy.RevealContentHandler;

public class MyAccountPresenter extends PresenterWidget<MyAccountPresenter.MyView>
{

	private final CustomerServiceAsync service;
	
	private final PlaceManager placeManager;
	
	private CardPresenter cardPresenter;
	
	@ContentSlot
	public static final Type<RevealContentHandler<?>> slot = new Type<RevealContentHandler<?>>();
	
	
	@Inject 
	public MyAccountPresenter(EventBus eventBus, MyView view,CardPresenter cardPresenter, CustomerServiceAsync service,PlaceManager placeManager) {
		super(eventBus, view);
		this.placeManager=placeManager;
		this.service = service;
		this.cardPresenter = cardPresenter;
		
	}

	
	public interface MyView extends View {

		void showMyAccountTab(GWT_Customer result);
		HasClickHandlers submitNameChange();
		HasText getNameValue();
		void updatePostNameChanged();
		HasClickHandlers submitEmailButton();
		HasText getEmailValue();
		void updatePostEmailChanged();
		HasText getCurrentPasswdValue();
		HasText getNewPasswdValue();
		void updatePostPasswdChanged();
		void updatePostPasswdFailure();
		HasClickHandlers submitPasswdButton();
		FormPanel getUploadForm();
		void updatePhoto(String filename,Double ratio);
	}
	
	@Override
	public void onReveal() {
		Log.debug("MyAccountPresenter onReveal");
		this.cardPresenter.getView().setContext("myaccount");
		service.getCurrentCustomer(new AsyncCallback<GWT_Customer>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(GWT_Customer result) {
				getView().showMyAccountTab(result);
			}
			
		});
				
		setInSlot(slot,cardPresenter);
		
	}
	
	@Override
	public void onBind() {
		super.onBind();
		Log.debug("MyAccountPresenter onBind");
		registerHandler(getView().submitNameChange().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				service.updateName(getView().getNameValue().getText(),new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Boolean result) {
						getView().updatePostNameChanged();
						NameChangeEvent.fire(getEventBus(), getView().getNameValue().getText());
					}
					
				});
			}
			
		}));
		
		registerHandler(getView().submitEmailButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				service.updateEmail(getView().getEmailValue().getText(),new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						
					}

					@Override
					public void onSuccess(Boolean result) {
						getView().updatePostEmailChanged();
					}
					
				});
			}
			
		}));
		
		registerHandler(getView().submitPasswdButton().addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				service.updatePasswd(getView().getCurrentPasswdValue().getText(),getView().getNewPasswdValue().getText(),new AsyncCallback<Boolean>() {

					@Override
					public void onFailure(Throwable caught) {
						// TODO Auto-generated method stub
						
					}

					@Override
					public void onSuccess(Boolean result) {
						if(result)
							getView().updatePostPasswdChanged();
						else
							getView().updatePostPasswdFailure();
					}
					
				});
			}
			
		}));
		
		registerHandler(getView().getUploadForm().addSubmitCompleteHandler(new SubmitCompleteHandler() {

			@Override
			public void onSubmitComplete(SubmitCompleteEvent event) {
				Log.debug(event.getResults());
				 JSONObject response = (JSONObject) JSONParser.parse(event.getResults());
				 
				 String filename = ((JSONString) response.get("filename")).stringValue();
				 String ratio = ((JSONString) response.get("ratio")).stringValue();
				 UploadPhotoEvent.fire(getEventBus(), filename);
				 getView().updatePhoto(filename, Double.parseDouble(ratio));
			}
			
		}));
	}
	
	@Override
	public void onUnbind() {
		super.onUnbind();
		
	}
	
	@Override
	public void onReset() {
		super.onReset();
		Log.debug("MyAccountPresenter onReset");
	}
	

}
